package com.anhtuan.reminder.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class TasksProviderWrapper {
	private static String[] sTasksProjection = new String [] {
		TaskColumns._ID,
		TaskColumns.ACTIVE,
		TaskColumns.ACTION,
		TaskColumns.REPEAT,
		TaskColumns.DAY,
		TaskColumns.FIRST_CONTENT,
		TaskColumns.SECOND_CONTENT,
		TaskColumns.THIRD_CONTENT
	};
	
	private static Uri TASKS_URI = TasksContentProvider.CONTENT_URI;
	
	public static List<TaskItem> getAllTasks(ContentResolver contentResolver) {
		List<TaskItem> result = new ArrayList<TaskItem>();
		
		Cursor cursor = contentResolver.query(TASKS_URI, sTasksProjection, null, null, null);
		
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				do {
					TaskItem item = new TaskItem(
							cursor.getInt(cursor.getColumnIndex(TaskColumns.ACTIVE)) == 1,
							cursor.getInt(cursor.getColumnIndex(TaskColumns.ACTION)),
							cursor.getInt(cursor.getColumnIndex(TaskColumns.REPEAT)),
							cursor.getInt(cursor.getColumnIndex(TaskColumns.DAY)),
							cursor.getString(cursor.getColumnIndex(TaskColumns.FIRST_CONTENT)),
							cursor.getString(cursor.getColumnIndex(TaskColumns.SECOND_CONTENT)),
							cursor.getString(cursor.getColumnIndex(TaskColumns.THIRD_CONTENT)));
					item.setId(cursor.getInt(cursor.getColumnIndex(TaskColumns._ID)));
					result.add(item);
				} while (cursor.moveToNext());
			}
			cursor.close();
		}
		
		return result;
	}
	
	public static TaskItem getTaskById(ContentResolver contentResolver, int id) {
		TaskItem result = null;
		String whereClause = TaskColumns._ID + " = " + id;
		
		Cursor cursor = contentResolver.query(TASKS_URI, sTasksProjection, whereClause, null, null);
		
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				result = new TaskItem(
						cursor.getInt(cursor.getColumnIndex(TaskColumns.ACTIVE)) == 1,
						cursor.getInt(cursor.getColumnIndex(TaskColumns.ACTION)), 
						cursor.getInt(cursor.getColumnIndex(TaskColumns.REPEAT)),
						cursor.getInt(cursor.getColumnIndex(TaskColumns.DAY)),
						cursor.getString(cursor.getColumnIndex(TaskColumns.FIRST_CONTENT)), 
						cursor.getString(cursor.getColumnIndex(TaskColumns.SECOND_CONTENT)),
						cursor.getString(cursor.getColumnIndex(TaskColumns.THIRD_CONTENT)));
				result.setId(id);
			}
			
			cursor.close();
		}
		
		return result;
	}
	
	public static int getLastestId(ContentResolver contentResolver) {
		int result = -1;
		
		String orderClause = TaskColumns._ID + " DESC";
		Cursor cursor = contentResolver.query(TASKS_URI, new String[] {TaskColumns._ID}, null, null, orderClause);
		
		if (cursor != null) {
			if (cursor.moveToFirst()) {
				result = cursor.getInt(cursor.getColumnIndex(TaskColumns._ID));
			}
			cursor.close();
		}
		
		return result;
	}
	
	public static void insertRowRecord(ContentResolver contentResolver, TaskItem taskItem) {
		ContentValues values = new ContentValues();
		values.put(TaskColumns.ACTIVE, true);
		values.put(TaskColumns.ACTION, taskItem.getAction());
		values.put(TaskColumns.REPEAT, taskItem.getRepeat());
		values.put(TaskColumns.DAY, taskItem.getDay());
		values.put(TaskColumns.FIRST_CONTENT, taskItem.getFirstContent());
		values.put(TaskColumns.SECOND_CONTENT, taskItem.getSecContent());
		values.put(TaskColumns.THIRD_CONTENT, taskItem.getThirdContent());
		
		contentResolver.insert(TASKS_URI, values);
	}
	
	public static void deleteRecordById(ContentResolver contenResolver, int id) {
		String whereClause = TaskColumns._ID + " = " + id;
		contenResolver.delete(TASKS_URI, whereClause, null);
	}
	
	public static void deleteAllRecord(ContentResolver contentResolver) {
		contentResolver.delete(TASKS_URI, null, null);
	}
	
	public static void updateRowRecord(ContentResolver contenResolver, TaskItem taskItem) {
		String whereClause = TaskColumns._ID + " = " + taskItem.getId();
		
		ContentValues values = new ContentValues();
		values.put(TaskColumns.ACTIVE, taskItem.isActive());
		values.put(TaskColumns.ACTION, taskItem.getAction());
		values.put(TaskColumns.REPEAT, taskItem.getRepeat());
		values.put(TaskColumns.DAY, taskItem.getDay());
		values.put(TaskColumns.FIRST_CONTENT, taskItem.getFirstContent());
		values.put(TaskColumns.SECOND_CONTENT, taskItem.getSecContent());
		values.put(TaskColumns.THIRD_CONTENT, taskItem.getThirdContent());
		
		contenResolver.update(TASKS_URI, values, whereClause, null);
	}
	
	public static void setActive(ContentResolver contentResolver, int taskId, boolean active) {
		String whereClause = TaskColumns._ID + " = " + taskId;
		
		ContentValues values = new ContentValues();
		values.put(TaskColumns.ACTIVE, active);
		
		contentResolver.update(TASKS_URI, values, whereClause, null);
	}
}
